package com.zhongshengnetwork.rightbe.my.activity;

import android.content.Context;
//import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.loading.KyLoadingBuilder;
import com.zhongshengnetwork.rightbe.common.utils.ToastUtil;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;

import java.util.HashMap;
import java.util.Map;
//三检查OK:2016-11-30
public class FeedBackActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener{
    private TopBarView topbar;
    EditText content;
    EditText mobile;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);
        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);
        content = (EditText) findViewById(R.id.activity_feed_back_content);
        mobile = (EditText) findViewById(R.id.activity_feed_back_mobile);
        topbar.getRightTextView().setVisibility(View.VISIBLE);
        topbar.getRightTextView().setText("提交");
        topbar.getRightTextView().setTextSize(16);
        topbar.getRightTextView().setTextColor(getResources().getColor(R.color.mainColor));
        topbar.getRightTextView().setClickable(true);
        topbar.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (content.getText().toString().trim().length() == 0){
                    ToastUtil.show(FeedBackActivity.this,"请输入反馈内容");
                }else {
                    hintKeyBoard();
                    if (mobile.getText().length() > 0 && mobile.getText().length() < 11){
                        Toast.makeText(FeedBackActivity.this, R.string.accont_error_tip, Toast.LENGTH_SHORT).show();
                        return;
                    }
                    KyLoadingBuilder dialog = new KyLoadingBuilder(FeedBackActivity.this);
                    dialog.setIcon(R.drawable.loading04);
                    dialog.setText("正在提交...");
                    dialog.setOutsideTouchable(false);//点击空白区域是否关闭
                    dialog.setBackTouchable(true);//按返回键是否关闭
                    dialog.show();
                    final Map<String,String> map = new HashMap<String, String>();
                    map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
                    map.put(APIKey.mobileKey, CommonUtils.formatString(mobile.getText().toString()));
                    map.put(APIKey.contentKey,content.getText().toString());
                    HttpsUtils.feedDo(map, new HttpCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            dialog.dismiss();
                            CommonModel commonModel = GsonTools.getCommonModel(data);
                            if (commonModel.getFlag().equals("1")){
                                ToastUtil.show(FeedBackActivity.this,"提交成功");
                                finish();
                            }else {
                                CustomApplication.showTip(commonModel,FeedBackActivity.this);
                            }
                        }

                        @Override
                        public void onError(String msg) {
                            dialog.dismiss();
                        }
                    });
                }
            }
        });
    }


    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        //Toast.makeText(LoginActivity.this, "你点击了左侧按钮", Toast.LENGTH_LONG).show();
        hintKeyBoard();
        finish();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

    public void hintKeyBoard() {
        //拿到InputMethodManager
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        //如果window上view获取焦点 && view不为空
        if (imm.isActive() && getCurrentFocus() != null) {
            //拿到view的token 不为空
            if (getCurrentFocus().getWindowToken() != null) {
                //表示软键盘窗口总是隐藏，除非开始时以SHOW_FORCED显示。
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

}
